package adapter.user_management;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import custom_view.InviteUserTabLinearLayout;
import de.hdodenhof.circleimageview.CircleImageView;
import define.API;
import model.user_management.AllTabInUserManagementItem;
import model.user_management.UserInfoFromContactItem;
import ui.activity.user_managament.InviteUserToCompany;
import ui.fragment.group_management.AddMemberToGroupFragment;
import utils.Utils;


/**
 * Created by trek2000 on 21/8/2014.
 */
public class SuggestUserInInviteUserToCompanyAdapter extends BaseAdapter {

    /**
     * Data section
     */
    private ArrayList<UserInfoFromContactItem> mAlUserInfoFromContact = new ArrayList<>();

    /**
     * Interface section
     */

    /**
     * String section
     */
    private int textViewResourceId;
    private int pos_of_checked_item = -1;
    public static ArrayList <Integer> mAlPosOfItemCheckedInSuggestUser=  new ArrayList<>();
    private Boolean IS_ADD_GROUP_MANAGER = false;
    /**
     * The other section
     */
    private Context mContext;
    private DisplayImageOptions mDio;
    private ViewHolder viewHolder;

    private class ViewHolder {
        private CircleImageView circleIvItem;
        private TextView mTvUserName;
        private TextView mTvUserEmail;
        private CheckBox mCbItem;
    }

    /**
     * @param context
     */
    public SuggestUserInInviteUserToCompanyAdapter(Context context, int textViewResourceId,
                                                   ArrayList<UserInfoFromContactItem> mAluserInfoFromContact
                                                   ) {
        this.mContext = context;
        this.textViewResourceId = textViewResourceId;
        this.mAlUserInfoFromContact = mAluserInfoFromContact;

        mDio = new DisplayImageOptions.Builder()
                .delayBeforeLoading(1000)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    /**
     * @return
     */
    public int getCount() {
        return mAlUserInfoFromContact.size();
    }

    /**
     * @param position
     * @return
     */
    public UserInfoFromContactItem getItem(int position) {
        return mAlUserInfoFromContact.get(position);
    }

    /**
     * @param position
     * @return
     */
    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int pos, View convertView, ViewGroup parent) {
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(textViewResourceId, null);
            viewHolder.circleIvItem = (CircleImageView) convertView.findViewById(R.id.circleiv_item_in_simple_list_item_in_suggest_user_in_invite_user_to_company_activity);
            viewHolder.mTvUserName = (TextView) convertView.findViewById(R.id.tv_user_name_in_simple_list_item_in_suggest_user_in_invite_user_to_company_activity);
            viewHolder.mTvUserEmail = (TextView) convertView.findViewById(R.id.tv_user_email_in_simple_list_item_in_suggest_user_in_invite_user_to_company_activity);
            viewHolder.mCbItem = (CheckBox) convertView.findViewById(R.id.cb_item_in_simple_list_item_in_suggest_user_in_invite_user_to_company_activity);

            viewHolder.circleIvItem.setTag(pos);
            viewHolder.mTvUserName.setTag(pos);
            viewHolder.mTvUserEmail.setTag(pos);
            viewHolder.mCbItem.setTag(pos);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();

            viewHolder.circleIvItem.setTag(pos);
            viewHolder.mTvUserName.setTag(pos);
            viewHolder.mTvUserEmail.setTag(pos);
            viewHolder.mCbItem.setTag(pos);
        }
        //set data
        if (mAlUserInfoFromContact.get(pos).getUserAvatar() != null) {
            viewHolder.circleIvItem.setImageBitmap(
                    mAlUserInfoFromContact.get(pos).getUserAvatar()
            );
        } else viewHolder.circleIvItem.setImageResource(R.drawable.iv_default_avatar);
        viewHolder.mTvUserName.setText(
                mAlUserInfoFromContact.get(pos).getUserName());
        viewHolder.mTvUserEmail.setText(
                mAlUserInfoFromContact.get(pos).getUserEmail());
        // on click listener
        viewHolder.mCbItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = ((CheckBox) v).isChecked();
                switch (v.getId()) {
                    case R.id.cb_item_in_simple_list_item_in_suggest_user_in_invite_user_to_company_activity:
                        if (checked) {
                            //check mail empty, mail valid and mail existed
                            if (!InviteUserToCompany.mAlInviteUserEmail.contains(
                                    mAlUserInfoFromContact.get(pos).getUserEmail())) {
                                InviteUserToCompany.mAlInviteUserEmail.add(
                                        mAlUserInfoFromContact.get(pos).getUserEmail());

                                //add the latest new mail in array to flow layout
                                //add to position ahead position of edit text input mail
                                InviteUserToCompany.mflowlayoutInInviteUserToCompany.addView(
                                        InviteUserTabLinearLayout.newInstance(
                                                mContext,
                                                InviteUserToCompany.mAlInviteUserEmail.get(
                                                        InviteUserToCompany.mAlInviteUserEmail.size() - 1)),
                                        InviteUserToCompany.mflowlayoutInInviteUserToCompany.getChildCount() - 1);
                                //set flag for each tab ==  position data of tab in array
                                InviteUserToCompany.mflowlayoutInInviteUserToCompany.
                                        getChildAt(InviteUserToCompany.mAlInviteUserEmail.size() - 1).
                                        setTag(InviteUserToCompany.mAlInviteUserEmail.size() - 1);
                                //clear text after input email success
                                InviteUserToCompany.mEtInputUserEmail.setText("");
                                //set text for mTvInviteInInviteUserToCompany
                                InviteUserToCompany.setTextForTextViewInviteInInviteUserToCompany(
                                        InviteUserToCompany.mAlInviteUserEmail);
                                //save pos of item checked to reset view
                                mAlPosOfItemCheckedInSuggestUser.add(pos);
                            } else {
                                Toast.makeText(mContext.getApplicationContext(),
                                        mContext.getString(R.string.toast_check_email_in_edit_text),
                                        Toast.LENGTH_LONG).show();
                            }
                        } else {
                            //remove view, compare email in array invite with email in array check box checked
                            for (int e = 0; e < InviteUserToCompany.mAlInviteUserEmail.size() ; e++) {
                                if (InviteUserToCompany.mAlInviteUserEmail.get(e).equals(
                                        mAlUserInfoFromContact.get(pos).getUserEmail()
                                )) {
                                    InviteUserToCompany.mflowlayoutInInviteUserToCompany.removeViewAt(
                                            e
                                    );
                                    //remove data
                                    InviteUserToCompany.mAlInviteUserEmail.remove(
                                            InviteUserToCompany.mAlInviteUserEmail.get(
                                                    e
                                            )
                                    );
                                    //reset tag for each item of flow layout after remove one item
                                    for (int t = e;
                                         t < InviteUserToCompany.mflowlayoutInInviteUserToCompany.getChildCount() - 1; t++) {
                                        InviteUserToCompany.mflowlayoutInInviteUserToCompany.getChildAt(t).setTag(t);
                                    }
                                    //set text for mTvInviteInInviteUserToCompany
                                    InviteUserToCompany.setTextForTextViewInviteInInviteUserToCompany(
                                            InviteUserToCompany.mAlInviteUserEmail);
                                }
                            }
                            //remove pos of item checked to reset view
                            for (int p = 0; p < mAlPosOfItemCheckedInSuggestUser.size(); p++) {
                                if (mAlPosOfItemCheckedInSuggestUser.get(p) == pos) {
                                    mAlPosOfItemCheckedInSuggestUser.remove(p);
                                }
                            }
                        }
                        break;
                }
            }
        });
        // reset all item is false, after set one item is checked
        ((CheckBox) viewHolder.mCbItem.findViewWithTag(pos)).setChecked(false);
        if (mAlPosOfItemCheckedInSuggestUser.size() > 0) {
            for (int c = 0 ; c < mAlPosOfItemCheckedInSuggestUser.size(); c++) {
                if (pos == mAlPosOfItemCheckedInSuggestUser.get(c))
                    ((CheckBox) viewHolder.mCbItem.findViewWithTag(pos)).setChecked(true);
            }
        }
        return convertView;
    }
}